﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[proc_Company_ByMid]
	@mid INT,
	@userid VARCHAR(20)
AS
BEGIN
	
	DECLARE @cid VARCHAR(8000)
	SELECT @cid=au.Cid FROM aspnet_Users au where au.username =@UserId
	IF @cid='[ALL]'
	BEGIN
		  SELECT 
 
  	@cid=
  	REPLACE(REPLACE(( SELECT id FROM Company  
                                        order by id asc FOR XML AUTO ), '<Company id="', '['), '"/>', ']')
	END
	ELSE IF @cid=''
	BEGIN
		SELECT @cid= '['+Convert(varchar,au.CompanyId)+']'
		  FROM aspnet_Users au where au.username =@UserId
	END


	SELECT c.Id AS companyid, c.CompanyName
	 FROM Company c WHERE mid =@mid AND   CHARINDEX('['+CONVERT(VARCHAR, id)+']' , 
                       @cid,0)>0
                   
                       
END
